package com.example2016.cjj.wxplangamedemo.game;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;

import com.example2016.cjj.wxplangamedemo.view.GameView;

import java.util.List;

/**
 * Created by CJJ on 2017/3/26.
 */

public class EnemyPlane extends AutoSprite {

    //敌机抗击打能力
    private int power = 1;

    //打一个敌机的得分
    private int score = 0;


    public EnemyPlane(Bitmap bitmap) {
        super(bitmap);
    }


    @Override
    protected void afterDraw(Canvas canvas, Paint paint, GameView gameView) {
        super.afterDraw(canvas, paint, gameView);

        //绘制完成后要检查自身是否被子弹打中
        if( !isDestroy() ){
            List<Bullet> bullets = gameView.getAliveBullets();
            for( Bullet bullet : bullets){
                //判断敌机是否与子弹相交
                Point p = getCollidePointWithOther(bullet);
                if( p!=null ){
                    //有相交，说明子弹打到敌机上，敌机血量减少
                    bullet.destroy();
                    power--;
                    if( power <=0){
                        //敌机已经没有血量了，执行爆炸效果
                        explode(gameView);
                        return;
                    }

                }
            }
        }


    }

    //爆炸效果后，销毁敌机
    public void explode(GameView gameView) {
        //创建爆炸效果
        Bitmap explosionBitmap = gameView.getExplosinBitmap();
        Explosion explosion = new Explosion(explosionBitmap);
        explosion.moveTo(getX(),getY());  //移动到当前敌机的x，y坐标
        //添加在gameview中
        gameView.addNeededSprite(explosion);
        //添加得分，并销毁
        gameView.addScore(score);
        destroy();
    }


    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public int getPower() {
        return power;
    }

    public void setPower(int power) {
        this.power = power;
    }
}
